// div 64/32 implementation

/*
   Copyright 2006 Corey Tabaka

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

unsigned int __div64_32(unsigned long long *n, unsigned long base) {
	unsigned long long rem = *n;
	unsigned long long b = base;
	unsigned long long res, d = 1;
	unsigned int high = rem >> 32;

	/* Reduce the thing a bit first */
	res = 0;
	if (high >= base) {
		high /= base;
		res = (unsigned long long) high << 32;
		rem -= (unsigned long long) (high*base) << 32;
	}

	while ((long long)b > 0 && b < rem) {
		b = b+b;
		d = d+d;
	}

	do {
		if (rem >= b) {
			rem -= b;
			res += d;
		}
		b >>= 1;
		d >>= 1;
	} while (d);

	*n = res;
	return rem;
}

unsigned long long __udivdi3(unsigned long long n, unsigned long long base) {
	unsigned long long rem = n;
	unsigned long long b = base;
	unsigned long long res, d = 1;
	unsigned int high = rem >> 32;

	/* Reduce the thing a bit first */
	res = 0;
	if (high >= base) {
		high /= base;
		res = (unsigned long long) high << 32;
		rem -= (unsigned long long) (high*base) << 32;
	}

	while ((long long)b > 0 && b < rem) {
		b = b+b;
		d = d+d;
	}

	do {
		if (rem >= b) {
			rem -= b;
			res += d;
		}
		b >>= 1;
		d >>= 1;
	} while (d);

	return res;
}

unsigned long long __umoddi3(unsigned long long n, unsigned long long base) {
	unsigned long long rem = n;
	unsigned long long b = base;
	unsigned long long res, d = 1;
	unsigned int high = rem >> 32;

	/* Reduce the thing a bit first */
	res = 0;
	if (high >= base) {
		high /= base;
		res = (unsigned long long) high << 32;
		rem -= (unsigned long long) (high*base) << 32;
	}

	while ((long long)b > 0 && b < rem) {
		b = b+b;
		d = d+d;
	}

	do {
		if (rem >= b) {
			rem -= b;
			res += d;
		}
		b >>= 1;
		d >>= 1;
	} while (d);

	return rem;
}

long long __divdi3(long long n, long long base) {
	long long rem = n;
	long long b = base;
	long long res, d = 1;
	int high = rem >> 32;

	/* Reduce the thing a bit first */
	res = 0;
	if (high >= base) {
		high /= base;
		res = (long long) high << 32;
		rem -= (long long) (high*base) << 32;
	}

	while ((long long)b > 0 && b < rem) {
		b = b+b;
		d = d+d;
	}

	do {
		if (rem >= b) {
			rem -= b;
			res += d;
		}
		b >>= 1;
		d >>= 1;
	} while (d);

	return res;
}

long long __moddi3(long long n, long long base) {
	long long rem = n;
	long long b = base;
	long long res, d = 1;
	int high = rem >> 32;

	/* Reduce the thing a bit first */
	res = 0;
	if (high >= base) {
		high /= base;
		res = (long long) high << 32;
		rem -= (long long) (high*base) << 32;
	}

	while ((long long)b > 0 && b < rem) {
		b = b+b;
		d = d+d;
	}

	do {
		if (rem >= b) {
			rem -= b;
			res += d;
		}
		b >>= 1;
		d >>= 1;
	} while (d);

	return rem;
}
